package com.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.model.shoppingcar.Shoppingcar;

/**
 * 购物车dao层
 * @author 飘仙人
 *
 */
public class ShoppingcarDao {
	private StringBuffer sql = null;
	
	/**
	 * 增加商品到购物车
	 * @param nShoppingcard
	 * @return
	 * @throws Exception
	 */
	public int addShoppingcar(Shoppingcar nShoppingcard) throws Exception{
		sql = new StringBuffer("");
		sql.append("insert into t_shoppingcar_info(userid,id,shopnum)values(")
		.append(nShoppingcard.getUserid()).append(",")
		.append(nShoppingcard.getId()).append(",")
		.append(nShoppingcard.getShopnum()).append(");");
		
		int iFlag = DBHelper.updateData(sql.toString());
		return iFlag;	
	}
	
	/**
	 * 查询购物车
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public List<Shoppingcar> findAll(String username) throws Exception{
		sql = new StringBuffer("");
		sql.append("select * from v_shoppingcar where username='").append(username).append("' and STATUS='未购买';");
		
		ResultSet rs = DBHelper.queryData(sql.toString());
		List<Shoppingcar> ShoppingcarList = new ArrayList<Shoppingcar>();
		
		while(rs.next()){
			Shoppingcar s = new Shoppingcar();
			
			s.setPicurl(rs.getString("picurl"));
			s.setUsername(rs.getString("username"));
			s.setGoodname(rs.getString("goodname"));
			s.setShopnum(rs.getInt("shopnum"));
			s.setPrice(rs.getDouble("price"));
			s.setMemberprice(rs.getDouble("memberprice"));
			s.setShopmoney(rs.getDouble("shopmoney"));
			
			ShoppingcarList.add(s);
		}
		return ShoppingcarList;
	}
}
